#!/bin/bash

set -eou pipefail

LDFLAGS="-X \"github.com/rancher/k3k/pkg/buildinfo.Version=${VERSION}\""

build_args=()

# Check if coverage is enabled, e.g., in CI or when manually set
if [[ "${COVERAGE:-false}" == "true" ]]; then
    echo "Coverage build enabled."
    build_args+=("-cover" "-coverpkg=./..." "-covermode=atomic")
fi

echo "Building k3k... [cli os/arch: $(go env GOOS)/$(go env GOARCH)]"
echo "Current TAG: ${VERSION} "

export CGO_ENABLED=0
GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" "${build_args[@]}" -o bin/k3k
GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" "${build_args[@]}" -o bin/k3k-kubelet ./k3k-kubelet

# build the cli for the local OS and ARCH
go build -ldflags="${LDFLAGS}" "${build_args[@]}" -o bin/k3kcli ./cli
